package qyf.example.eurekaclientelsaticsearch.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qyf.example.eurekaclientelsaticsearch.entity.Article;
import qyf.example.eurekaclientelsaticsearch.entity.Video;
import qyf.example.eurekaclientelsaticsearch.repository.ArticleRepository;
import qyf.example.eurekaclientelsaticsearch.repository.VideoRepository;

import java.util.List;

@RequestMapping("/search")
@RestController
@Slf4j
public class SearchController {

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private VideoRepository videoRepository;

    @RequestMapping("/art/getPage")
    public List<Article> getPage(String content, String title) {
        PageRequest pageRequest = PageRequest.of(0, 5);
        Page<Article> page = articleRepository.findByContentOrTitleOrderByCreateTimeDesc(content, title, pageRequest);
        List<Article> list = page.getContent();
        log.info("pageList:{}", list);
        return list;
    }

    @RequestMapping("/video/getPage")
    public List<Video> getPage(String title) {
        PageRequest pageRequest = PageRequest.of(0, 5);
        Page<Video> page = videoRepository.findByTitleOrderByUpdateTimeDesc(title, pageRequest);
        List<Video> videoList = page.getContent();
        log.info("pageList:{}", videoList);
        return videoList;
    }

}
